import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

import static java.util.Arrays.binarySearch;
import static java.util.Arrays.sort;

class KindergartenGarden {
    private static final ThreadLocal<AtomicInteger> PLANTERS;
    private static final ThreadLocal<AtomicInteger> STUDENT_GROUP;

    static {
        PLANTERS = ThreadLocal.withInitial(() -> new AtomicInteger(8));
        STUDENT_GROUP = ThreadLocal.withInitial(() -> new AtomicInteger(4));
    }

    private String[] plants;
    private String[] students;


    KindergartenGarden(String plants) {
        this.plants = plants.split("\n");
    }

    KindergartenGarden(String plants, String[] studentArray) {
        this(plants);
        this.students = studentArray;
        sort(students);
    }

    List<Plant> getPlantsOfStudent(String student) {
        List<Plant> result = new ArrayList<>();

        int groupIdx = (null != this.students) ? 0 :
                ((student.charAt(0) - 'A') / STUDENT_GROUP.get().get());
        int nameIdx = (null != this.students) ?
                binarySearch(this.students, student) : ((student.charAt(0) - 'A') % STUDENT_GROUP.get().get());

        for (String plant : this.plants) {
            result.add(Plant.getPlant(plant.charAt(groupIdx * PLANTERS.get().get() + nameIdx * 2)));
            result.add(Plant.getPlant(plant.charAt(groupIdx * PLANTERS.get().get() + nameIdx * 2 + 1)));
        }

        return result;
    }
}
